package com.woniuxueyuan.Controller;

import com.woniuxueyuan.entity.Book;
import com.woniuxueyuan.entity.KillBook;
import com.woniuxueyuan.service.SecKillService;
import com.woniuxueyuan.util.ResponseData;
import com.woniuxueyuan.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/seckill")
public class SecKillController {
    @Resource
    private SecKillService secKillService;
    /**
     * 接受书籍信息，放入redis中
     */
    @PostMapping("/addBook")
    public ResponseData addSecKillBook(@RequestBody KillBook killBook) throws ParseException {
        secKillService.addKillBook(killBook);
        return ResponseData.ok();
    }
    @GetMapping("/queryAll")
    public ResponseData queryAll(){
        List<KillBook> killBooks = secKillService.queryAll();
        return ResponseData.ok(killBooks);

    }
    @GetMapping("/queryDetail/{id}")
    public  ResponseData queryDetail(@PathVariable("id") Integer id){
        KillBook killBook = secKillService.queryDetail(id);
        return ResponseData.ok(killBook);

    }
    @GetMapping("/startKill/{id}")
    public ResponseData startKill(@PathVariable("id") Integer id){
        Boolean aBoolean = secKillService.startKill(id);
       if(aBoolean){
           return  ResponseData.ok();
       }else {
           return ResponseData.fail(ResponseEnum.SECKILL_END);//提示抢购结束
       }



    }
}
